using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MonoCash.ObjectModel;

namespace MonoCash
{
    public partial class SummaryView : Form
    {
        public SummaryView()
        {
            InitializeComponent();

            DrawAccountTable();



        }

        void DrawAccountTable()
        {
            tlpAccounts.Controls.Clear();

            List<List<Account>> accounts = new List<List<Account>>();
            accounts.Add(new List<Account>(Account.GetAccountsByTypeName("Checking")));
            accounts.Add(new List<Account>(Account.GetAccountsByTypeName("Savings")));
            accounts.Add(new List<Account>(Account.GetAccountsByTypeName("Investment")));
            accounts.Add(new List<Account>(Account.GetAccountsByTypeName("Loan")));
            accounts.Add(new List<Account>(Account.GetAccountsByTypeName("Credit Card")));
            accounts.Add(new List<Account>(Account.GetAccountsByTypeName("Liability")));

            string format = "0.00";
            int rowCounter = 0;
            Decimal netWorth = 0;
            Label netWorthLabel = new Label();
            netWorthLabel.Text = "Net Worth";
            netWorthLabel.Font = new Font(netWorthLabel.Font, FontStyle.Bold);

            foreach (List<Account> accountGroup in accounts)
            {
                int headerRow = rowCounter;
                decimal headerBalance = 0;
                if (accountGroup.Count > 0)
                {
                    //Display Header
                    Label header = new Label();
                    header.Text = accountGroup[0].AccountType.Name + " Accounts";
                    header.AutoSize = true;
                    header.Anchor = AnchorStyles.Top | AnchorStyles.Left;
                    header.Font = netWorthLabel.Font;
                    tlpAccounts.RowCount = rowCounter + 1;
                    tlpAccounts.Controls.Add(header, 0, rowCounter);
                    tlpAccounts.SetColumnSpan(header, 2);
                    rowCounter++;
                }
                foreach (Account account in accountGroup)
                {
                    //Display name and balance
                    LinkLabel name = new LinkLabel();
                    name.Text = account.Name;
                    name.Anchor = Anchor = AnchorStyles.Top | AnchorStyles.Left;
                    name.Tag = account;//For launching transaction editor
                    name.LinkClicked += new LinkLabelLinkClickedEventHandler(name_LinkClicked);
                    Label balance = new Label();
                    Decimal accountBalance = account.DisplayBalance;
                    netWorth += account.Balance;
                    headerBalance += accountBalance;
                    balance.Text = accountBalance.ToString(format);
                    balance.Anchor = Anchor = AnchorStyles.Top | AnchorStyles.Right;
                    balance.Location = new Point(0, 0);

                    tlpAccounts.RowCount = rowCounter + 1;
                    tlpAccounts.Controls.Add(name, 1, rowCounter);
                    tlpAccounts.Controls.Add(balance, 2, rowCounter);
                    rowCounter++;
                }
                if (accountGroup.Count > 0)
                {
                    Label balance = new Label();
                    balance.Text = headerBalance.ToString(format);
                    balance.Font = netWorthLabel.Font;
                    balance.Anchor = AnchorStyles.Right | AnchorStyles.Top;
                    tlpAccounts.Controls.Add(balance, 2, headerRow);

                    rowCounter++;
                }
            }

            Label netWorthValue = new Label();
            netWorthValue.Text = netWorth.ToString(format);
            netWorthValue.Font = netWorthLabel.Font;

            rowCounter++;
            tlpAccounts.Controls.Add(netWorthLabel, 0, rowCounter);
            tlpAccounts.SetColumnSpan(netWorthLabel, 2);
            tlpAccounts.Controls.Add(netWorthValue, 2, rowCounter);
        }


        void name_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            LinkLabel l = (LinkLabel)sender;
            TransactionEditor form = new TransactionEditor((Account)l.Tag);
            form.MdiParent = this.MdiParent;
            form.Show();


            //Transactions f = new Transactions();
            //f.MdiParent = this.MdiParent;
            //f.Show();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            DrawAccountTable();
        }
    }
}